var checkResult =  null, sliderComplete = false, fingerprintActive = null, wait = 10;
var container = document.querySelector('.container');
var oper_flag = document.querySelector("#oper-flag");
var name_input = document.querySelector("#name-input");
var pass_input = document.querySelector("#pass-input");
var code_input = document.querySelector("#code_input");
var pass_btn = document.querySelector("#oper-btn");
var pass_btn_text = document.querySelector("#oper-btn");
var captcha_enabled = document.querySelector("#captcha-enabled");


var p_line = '<p class="terminal__line line-line">--------------------------------------------------------</p>####刷新验证码';
var p_init = '<p class="terminal__line line-init">Initiating verification<span class="dots"><span>.</span><span>.</span><span>.</span></span></p>####正在初始化校验<span class="dots"><span>.</span><span>.</span><span>.</span></span>';
var p_inited = '<p class="terminal__line line-inited">verification interface completed.</p>####校验初始化完成.';
var p_fetch = '<p class="terminal__line line-fetch">Fetching latest verification code<span class="dots"><span>.</span><span>.</span><span>.</span></span></p>####获取最新校验码<span class="dots"><span>.</span><span>.</span><span>.</span></span>';
var p_fetched = '<p class="terminal__line line-fetched">Latest verification code fetched:</p>####.';
var p_kaptcha = '<div class="kaptcha-content2 line-kaptcha">\n' +
    '   <div class="mask_img_bg2">\n' +
    '       <img id="imgcode2" src="'+ctx+'osfw/captcha/captchaImage" width="100%" height="100%" />\n' +
    '   </div>\n' +
    '   <img id="redux2" src="'+ctx+'osfw/framework/login/eraser/img/layer.png" width="100%" height="100%" />\n' +
    '</div>####最新校验码已获取';
var p_enter = '<p class="terminal__line line-enter">Please enter the verification code:</p>####请刮开验证码，并输入验证码：<i></i>';


/**** 账号输入框失去焦点，获取焦点事件监听开始 ****/
var _date, timeline = 1500, _date2, time,
    fοcusStatus = false;
function _fοcus() {
    fοcusStatus = true;
}
function _blur() {
    fοcusStatus = false;
    if (oper_flag.value == "guan-bi-ren-zheng" && $.trim(name_input.value) == "") {
        $(".ticket1").hide(); // api-key输入框隐藏
        $(".ticket2").css("display", "none"); //token彩色块隐藏
        $(".ticket2 .password").blur(); // token数据域失去焦点
        $(".head").removeClass("head-open"); //上面板上滑关闭
        $(".ticket1").removeClass("ticket1-open-2"); //输入框右滑归位
        $(".ticket1").removeClass("ticket1-open-1"); //输入框右滑归位
        $(".body").removeClass("body-open"); //下面板下滑关闭
        $("#fw-login-msg").removeClass().text(""); // 提示
        $(".profile .pass-emoji").hide(); // 头像隐藏
        $("#pass-emoji-login").show(); // 灰色头像显示
        $(".oper-btn").removeClass("oper-btn-open-2"); // 按钮上滑移动
        $(".oper-btn").removeClass("oper-btn-open-1"); // 按钮左滑移动
        $(".oper-btn").show();

        oper_flag.value = "kai-qi-ren-zheng";
        pass_btn_text.innerHTML = "開啓認證";
    }
}
/**** 账号输入框失去焦点，获取焦点事件监听结束 ****/


/**** 账号输入监听开始 ****/
$('input.loginName').on('keydown', function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13 || code == 27) {
        return;
    }
    _date = new Date().getTime();
});
$('input.loginName').on('keyup', function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13 || code == 27 ) {
        return;
    }
    if (oper_flag.value == "disable") {
        return;
    }
    if (oper_flag.value == "guan-bi-ren-zheng") {
        if ($.trim(name_input.value) != "") {
            oper_flag.value = "jin-ru-ren-zheng";
            pass_btn_text.innerHTML = "進入認證";
        } else {
            oper_flag.value = "guan-bi-ren-zheng";
            pass_btn_text.innerHTML = "關閉認證";
        }
    } else if (oper_flag.value == "jin-ru-ren-zheng") {
        if ($.trim(name_input.value) == "") {
            oper_flag.value = "guan-bi-ren-zheng";
            pass_btn_text.innerHTML = "關閉認證";
        }
    } else if (oper_flag.value != "jin-ru-ren-zheng") {
        if ($.trim(name_input.value) === "" && pass_input.value === "") {
            oper_flag.value = "tui-chu-ren-zheng";
            pass_btn_text.innerHTML = "退出認證";
        } else {
            oper_flag.value = "ren-zheng-shen-fen";
            pass_btn_text.innerHTML = "認證身份";
        }
    }
    /*if (fοcusStatus && oper_flag.value == "jin-ru-ren-zheng") {
        time = setTimeout(() => {
            if (oper_flag.value == "jin-ru-ren-zheng") {
                _date2 = new Date().getTime();
                var _date3 = _date2 - _date;
                if (_date3 >= timeline) {
                    oper();
                }
            }
        }, timeline);
    }*/
});
/**** 账号输入监听结束 ****/

/**** 验证码输入监听开始 ****/
$('.search__input').on("keyup", function (e) {
    e.stopPropagation();
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13 || code == 27 ) {
        return;
    }
    $("#fw-vcode-msg").removeClass().html((($.common.isEmpty($(this).val())) ? "请输入验证码：" : "输入：")+$(this).val()+'<i></i>');
})
/**** 验证码输入监听结束 ****/

$('#vcode_refresh').click(function () {
    // $('.kaptcha-content2 > img:last').eraser('reset');
    // var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
    // $('.mask_img_bg2 > img:last').attr("src", url);
    refreshVcode();
});
$('#btn-search-close').click(function () {
    closeSearch();
});

function refreshVcode() {
    var line_arr = new Array();
    line_arr.push(p_line);
    line_arr.push(p_fetch);
    line_arr.push(p_fetched);
    line_arr.push(p_kaptcha);
    line_arr.push(p_enter);
    // 加载命令行
    appendTerminal(0, line_arr, function () {
    });
}

/**** 操作按钮点击操作开始 ****/
pass_btn.addEventListener('click', (ev) => {
    if (oper_flag.value == "disable" || $('.search').hasClass('search--open')) {
        ev.stopPropagation()
        return;
    }
    oper();
});
/**** 操作按钮点击操作结束 ****/


/**** 全局按键监听开始 ****/
var _keydown_date = new Date().getTime(),
    keydown_timeline = 600,
    _keydown_date2;
document.onkeydown = function (e) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    var target = theEvent.target;
    if($('.search').hasClass('search--open')) {
        if( code == 13 && $.common.isNotEmpty($('.search__input').val())) {
            $('.terminal').append('<p class="terminal__line append am-text-primary">#'+$('.search__input').val()+'</p>');
            code_input.value = $('.search__input').val();
            $('.search__input').val("");
            verifyCode();
            e.stopPropagation();
        }else if( code == 27 ) {
            closeSearch();
            e.stopPropagation();
        }
    }else {
        if($('.search').hasClass('search--open')) {
            alert(13123)
        }
        if (code == 13 || code == 27 || code == 9) {
            _keydown_date2 = new Date().getTime();
            var _date3 = _keydown_date2 - _keydown_date;
            if (_date3 >= keydown_timeline) {
                if (code == 13 || (code == 9 && $(target).hasClass("loginName") && oper_flag.value == "jin-ru-ren-zheng")) {
                    if (oper_flag.value == "disable") {
                        return;
                    }
                    oper();
                    e.preventDefault();
                } else if (code == 27 && oper_flag.value == "guan-bi-ren-zheng" && $.trim(name_input.value) == "") {
                    $(".ticket1 .loginName").blur(); // api-keyapi-key输入框获取焦点
                    $(".ticket1").hide(); // api-key输入框隐藏
                    $(".ticket1 .loginName").blur(); // api-keyapi-key输入框获取焦点
                    $(".ticket2").css("display", "none"); //token彩色块隐藏
                    $(".ticket2 .password").blur(); // token数据域失去焦点
                    $(".head").removeClass("head-open"); //上面板上滑关闭
                    $(".ticket1").removeClass("ticket1-open-2"); //输入框右滑归位
                    $(".ticket1").removeClass("ticket1-open-1"); //输入框右滑归位
                    $(".body").removeClass("body-open"); //下面板下滑关闭
                    $("#fw-login-msg").removeClass().text(""); // 提示
                    $(".profile .pass-emoji").hide(); // 头像隐藏
                    $("#pass-emoji-login").show(); // 灰色头像显示
                    $(".oper-btn").show();
                    $(".oper-btn").removeClass("oper-btn-open-2"); //输入框右滑归位
                    $(".oper-btn").removeClass("oper-btn-open-1"); //输入框右滑归位
                    oper_flag.value = "kai-qi-ren-zheng";
                    pass_btn_text.innerHTML = "開啓認證";
                }
                _keydown_date = new Date().getTime();
            }
            if($(target).hasClass("loginName") && oper_flag.value != "ren-zheng-shen-fen"){
                e.preventDefault();
            }
        }
    }

}
/**** 全局按键监听结束 ****/



/**** 密码输入事件开始 ****/
$('textarea.pass').keydown(function (event) {
    var theEvent = window.event || e;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13) {
        $(this).blur();
        _keydown_date2 = new Date().getTime();
        var _date3 = _keydown_date2 - _keydown_date;
        if (_date3 >= keydown_timeline) {
            if (oper_flag.value == "disable") {
                return;
            }
            oper();
            _keydown_date = new Date().getTime();
        }
        return false;
    }
});
/**** 密码输入事件结束 ****/


/**** 操作控制样式开始 ****/
function oper() {
    switch (oper_flag.value) {
        case "kai-qi-ren-zheng":
            $(".oper-btn").hide(); // 按钮隱藏
            $(".ticket1").show(); // api-key输入框显示
            $(".ticket1 .loginName").focus(); // api-keyapi-key输入框获取焦点
            $("#fw-login-msg").removeClass().text(""); // 提示
            $(".profile .pass-emoji").hide(); // 头像隐藏
            $("#pass-emoji-login").show(); // 灰色头像显示

            oper_flag.value = "guan-bi-ren-zheng";
            pass_btn_text.innerHTML = "關閉認證";
            break;
        case "guan-bi-ren-zheng":
            if ($.trim(name_input.value) == "") {
                $(".oper-btn").show(); // 按钮隱藏
                $(".ticket1").hide(); // api-key输入框隐藏
                $(".ticket1 .loginName").blur(); // api-keyapi-key输入框获取焦点
                $(".ticket2").css("display", "none"); //token彩色块隐藏
                $(".ticket2 .password").blur(); // token数据域失去焦点
                $(".ticket1").removeClass("ticket1-open-2"); //输入框右滑归位
                $(".ticket1").removeClass("ticket1-open-1"); //输入框右滑归位
                $(".head").removeClass("head-open"); //上面板上滑关闭
                $(".body").removeClass("body-open"); //下面板下滑关闭
                $("#fw-login-msg").removeClass().text(""); // 提示
                $(".profile .pass-emoji").hide(); // 头像隐藏
                $("#pass-emoji-login").show(); // 灰色头像显示

                oper_flag.value = "kai-qi-ren-zheng";
                pass_btn_text.innerHTML = "開啓認證";
            }
            break;
        case "jin-ru-ren-zheng":
            if ($.trim(name_input.value) != "") {
                $(".ticket1").addClass("ticket1-open-1"); //输入框左滑腾位
                $(".oper-btn").show(); // 按钮隱藏
                $(".oper-btn").addClass("oper-btn-open-1"); // 按钮右滑移动
                setTimeout(function () {
                    $("#fw-login-msg").removeClass().text(""); // 提示
                    $(".head").addClass("head-open"); //上面板上滑打开
                    $(".body").addClass("body-open"); //下面板下滑打开
                    $(".oper-btn").addClass("oper-btn-open-2"); // 按钮下滑移动
                    setTimeout(function () {
                        $(".ticket1").addClass("ticket1-open-2"); //输入框左滑腾位
                        $(".ticket2").css("display", "block"); //token彩色块显示
                        $(".ticket2 .password").focus(); // token数据域获取焦点
                        $("#fw-login-msg").removeClass().text(""); // 提示
                        $(".profile .pass-emoji").hide(); // 头像隐藏
                        $("#pass-emoji-login").show(); // 灰色头像显示
                    }, 300);
                }, 500);
                oper_flag.value = "ren-zheng-shen-fen";
                pass_btn_text.innerHTML = "認證身份";
            }
            break;
        case "tui-chu-ren-zheng":
            if ($.trim(name_input.value) == "" && pass_input.value == "") {
                $(".ticket2 .password").blur(); // token数据域失去焦点
                $(".ticket2").css("display", "none"); //token彩色块隐藏
                setTimeout(function () {
                    $(".oper-btn").removeClass("oper-btn-open-2"); // 按钮上滑移动
                    $(".head").removeClass("head-open"); //上面板上滑关闭
                    $(".body").removeClass("body-open"); //下面板下滑关闭
                    setTimeout(function () {
                        $(".ticket1").removeClass("ticket1-open-2"); //输入框右滑归位
                    }, 300);
                    setTimeout(function () {
                        $(".ticket1").removeClass("ticket1-open-1"); //输入框右滑归位
                        $(".oper-btn").removeClass("oper-btn-open-1"); // 按钮左滑移动
                        setTimeout(function () {
                            $(".oper-btn").hide(); // 按钮隱藏
                        }, 900);
                    }, 500);
                }, 100);
                $("#fw-login-msg").removeClass().text(""); // 提示
                $(".profile .pass-emoji").hide(); // 头像隐藏
                $("#pass-emoji-login").show(); // 灰色头像显示

                oper_flag.value = "guan-bi-ren-zheng";
                pass_btn_text.innerHTML = "關閉認證";
            }
            break;
        case "ren-zheng-shen-fen":
            if ($.trim(name_input.value) == "" || pass_input.value == "") {
                $(".ticket1 .loginName, .ticket2 .password, .ticket3 .loginKaptcha").blur();
                if ($.trim(name_input.value) == "") {
                    $(".ticket1 .loginName").focus(); // api-key输入框焦点
                    $("#fw-login-msg").removeClass().addClass("am-text-warning").text("请填写您的账号"); // 提示
                    $(".profile .pass-emoji").hide(); // 头像隐藏
                    $("#pass-emoji-wow").show(); // 头像显示
                } else if (pass_input.value == ""){
                    $(".ticket2 .password").focus(); // api-key输入框焦点
                    $("#fw-login-msg").removeClass().addClass("am-text-warning").text("请填写您的密码"); // 提示
                    $(".profile .pass-emoji").hide(); // 头像隐藏
                    $("#pass-emoji-wow").show(); // 头像显示
                }
                oper_flag.value = "ren-zheng-shen-fen";
                pass_btn_text.innerHTML = "認證身份";
            } else {
                $(".ticket1 .loginName, .ticket2 .password").blur();
                oper_flag.value = "ren-zheng-shen-fen";
                pass_btn_text.innerHTML = "認證身份";
                // 校验验证码
                if(captcha_enabled.value == 'yes') {
                    openSearch();
                }else {
                    doLogin();
                }
            }

            break;
        case "jin-ru-xi-tong":
            oper_flag.value = "disable";
            location.href = ctx + 'osfw/index';
            break;
    }
}
/**** 操作控制样式结束 ****/


/**** 验证面板js开始 ****/
function openSearch() {
    $('.vcode').removeClass('am-hide');
    $('.search').addClass('search--open');
    var line_arr = new Array();
    line_arr.push(p_init);
    line_arr.push(p_inited);
    line_arr.push(p_fetch);
    line_arr.push(p_fetched);
    line_arr.push(p_kaptcha);
    line_arr.push(p_enter);
    // 加载命令行
    appendTerminal(0, line_arr, function () {
    });
}

function appendTerminal(ts, line_arr, callback) {
    if (line_arr.length == ts) {callback(); return false;}
    var line = line_arr[ts];
    var line_p = line.split('####')[0];
    var line_msg = line.split('####')[1];
    var kaptcha_count = $('.terminal .kaptcha-content2').length;
    if(line_p.indexOf('kaptcha-content2') != -1 ) {
        line_p = line_p.replace('redux2', 'redux2-'+ (kaptcha_count + 1));
        line_p = line_p.replace('imgcode2', 'imgcode2-'+ (kaptcha_count + 1));
    }else if(line_p.indexOf('line-init') != -1) {
        setTimeout(function () {
            playShield("000000", null);
        }, 500);
    }
    $('.terminal').append(line_p);
    $("#fw-vcode-msg").removeClass().html(line_msg);
    ts++;
    if (line_arr.length == ts) setTimeout(function () {
        $('.kaptcha-content2 > img:last').eraser({
            size: 35,   //设置橡皮擦大小
            completeRatio: .5, //设置擦除面积比例
            // completeFunction: showResetButton   //大于擦除面积比例触发函数
        });
        var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
        $('.mask_img_bg2 > img:last').attr("src", url);
        $('.search__input').focus();
    }, 100);
    if(line_arr.length > 0 && (line_p.indexOf('line-fetched') != -1 || line_p.indexOf('line-kaptcha') != -1)) {
        setTimeout(function () {
            appendTerminal(ts, line_arr, callback);
            var terminal__line = $('.terminal__line span').parent();
            terminal__line.find('span').remove();
            terminal__line.append('...');
        }, 1000);
    }else if (line_arr.length > 0 && (line_p.indexOf('line-enter') != -1)) {
        setTimeout(function () {
            appendTerminal(ts, line_arr, callback);
            var terminal__line = $('.terminal__line span').parent();
            terminal__line.find('span').remove();
            terminal__line.append('...');
        }, 100);
    }else if (line_arr.length > 0) {
        setTimeout(function () {
            appendTerminal(ts, line_arr, callback);
            var terminal__line = $('.terminal__line span').parent();
            terminal__line.find('span').remove();
            terminal__line.append('...');
        }, (ts - 1) * 500 + 1000);
    }
}

function closeSearch() {
    $('.vcode').addClass('am-hide');
    $('.search').removeClass('search--open');
    $('.search__input').blur();
    $('.kaptcha-content2').addClass('am-hide');
    $('.terminal__line').remove();
    $('.search__input').val("");
    actionOut("#shield",'action_scaleOut',1,"");
    $("#fw-vcode-msg").removeClass().html("");
    $(".profile .pass-emoji").hide(); // 头像隐藏
    $("#pass-emoji-login").show(); // 灰色头像显示
    // $('.kaptcha-content2 > img:last').eraser('reset');
    // var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
    // $('.mask_img_bg2 > img:last').attr("src", url);
}
/**** 验证面板js结束 ****/

/**** 验证码js开始 ****/
function verifyCode() {
    $("#fw-login-msg").removeClass().addClass("am-text-warning").text("");
    $(".profile .pass-emoji").hide(); // 头像隐藏
    $("#pass-emoji-shield").show(); // 灰色头像显示
    $.ajax({
        type: "post",
        async: true,
        url: ctx + "osfw/captcha/checkCaptchaCode",
        data: {
            "imgCode": sm2.doEncrypt(code_input.value, publicKey)
        },
        beforeSend: function (request) {
            checkResult =  null; sliderComplete = false, fingerprintActive = null;
            setTimeout(function () {
                $('.terminal').append('<p class="terminal__line append">Verification code verification in progress<span class="dots"><span>.</span><span>.</span><span>.</span></span></p>')
                $("#fw-vcode-msg").removeClass().html('验证码校验中<span class="dots"><span>.</span><span>.</span><span>.</span></span>');
            }, 1000);
            setTimeout(function () {
                unlock(code_input.value, vCodeCallback);
            }, 1001);
        },
        success: function (res) {
            vCodeCallback("check", res);
        },
        error: function (xhr, status, error) {
            var errorMessage = "请求失败，请稍后再试。";
            if (xhr.status === 0) {
                errorMessage = "网络连接失败，请检查网络设置。";
            } else if (xhr.status === 404) {
                errorMessage = "请求的资源不存在。";
            } else if (xhr.status === 500) {
                // var responseText = JSON.parse(xhr.responseText);
                errorMessage = "服务器异常，请联系技术人员。";
            } else if (error === "timeout") {
                errorMessage = "请求超时，请稍后再试。";
            }
            checkResult = errorMessage;
            vCodeError();
            // 显示错误提示给用户
            $.modal.alertError(errorMessage);
        }
    });
}
function vCodeCallback(flag, res) {
    if(flag == "check") {
        if(res.code == web_status.SUCCESS) {
            if(res.data === true){
                checkResult =  true;
                if(sliderComplete) {
                    vCodeSuccess();
                }
            }else if(res.data === false) {
                checkResult =  false;
                if(sliderComplete) {
                    vCodeFail();
                }
            }
        }else {
            checkResult = res.msg;
            if(sliderComplete) {
                vCodeError();
            }
        }
    }else if(flag == "shield") {
        sliderComplete = true;
        if(checkResult == true) {
            vCodeSuccess();
        }else if(checkResult == false) {
            vCodeFail();
        }else if(checkResult != null) {
            vCodeError();
        }
    }
}
function vCodeSuccess() {
    setTimeout(function () {
        var terminal__line = $('.terminal__line span').parent();
        terminal__line.find('span').remove();
        terminal__line.append('...');
        $('.terminal').append('<p class="terminal__line append am-text-success">Pass the verification！</p>');
        $("#fw-vcode-msg").removeClass().addClass("am-text-success").html("验证码正确，验证通过！");
        shieldSuccess(function () {
            // $(".profile .pass-emoji").hide(); // 头像隐藏
            // $("#pass-emoji-user").show(); // 彩色头像显示
            closeSearch();
            doLogin();
        });
    }, 500);
}
function vCodeFail() {
    setTimeout(function () {
        var terminal__line = $('.terminal__line span').parent();
        terminal__line.find('span').remove();
        terminal__line.append('...');
        $('.terminal').append('<p class="terminal__line append am-text-danger">Verification code error, please re-enter！</p>');
        $("#fw-vcode-msg").removeClass().addClass("am-text-danger").html("验证码错误，请重新输入！");
        shieldFail(function () {
            // $(".profile .pass-emoji").hide(); // 头像隐藏
            // $("#pass-emoji-login").show(); // 灰色头像显示
            // $('#redux').eraser('reset');
            // var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
            // $("#imgcode").attr("src", url);
            // code_input.value = "";
            // 刷新验证码
            refreshVcode();
        });
    }, 1000);
}
function vCodeError() {
    setTimeout(function () {
        // 隐藏密码盾，提示异常信息
        var terminal__line = $('.terminal__line span').parent();
        terminal__line.find('span').remove();
        terminal__line.append('...');
        $('.terminal').append('<p class="terminal__line append am-text-danger">'+checkResult+'</p>');
        $("#fw-vcode-msg").removeClass().addClass("am-text-danger").html(checkResult);
        /*actionOut("#shield",'action_scaleOut',1,"", function () {
            // $(".profile .pass-emoji").hide(); // 头像隐藏
            // $("#pass-emoji-login").show(); // 灰色头像显示
            // $('#redux').eraser('reset');
            // var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
            // $("#imgcode").attr("src", url);
        });*/
        shieldFail(function () {
            // $(".profile .pass-emoji").hide(); // 头像隐藏
            // $("#pass-emoji-login").show(); // 灰色头像显示
            // $('#redux').eraser('reset');
            // var url = ctx + "osfw/captcha/captchaImage?s=" + Math.random();
            // $("#imgcode").attr("src", url);
            // code_input.value = "";
        });

    }, 1000);
}
/**** 验证码js结束 ****/



/**** 登录认证开始 ****/
function doLogin() {
    var _fingerprint_timeline = 1500;
    var _fingerprint_start = new Date().getTime();
    $.ajax({
        type: "post",
        async: true,
        url: ctx + "osfw/doLogin",
        data: {
            "phonenumber": sm2.doEncrypt($.trim(name_input.value), publicKey),
            "password": sm2.doEncrypt(pass_input.value, publicKey),
            "kaptcha": sm2.doEncrypt(code_input.value, publicKey)
        },
        beforeSend: function (request) {
            $("#fw-login-msg").removeClass().html('身份認證中<span class="dots"><span>.</span><span>.</span><span>.</span></span>');
            $(".profile .pass-emoji").hide(); // 头像隐藏
            $("#pass-emoji-fingerprint").show();
            $(".container").addClass("container-in");
            setTimeout(function () {
                container.classList.toggle('active');
                fingerprintActive = setInterval(function(){
                    container.classList.remove('active');
                    container.classList.toggle('active');
                },1501);
            }, 1);
        },
        success: function (res) {
            var _fingerprint_end = new Date().getTime();
            var _fingerprint_actived = _fingerprint_end - _fingerprint_start;
            if (_fingerprint_actived >= _fingerprint_timeline) {
                doLoginResultHandle(res);
            } else {
                setTimeout(function () {
                    doLoginResultHandle(res);
                }, (_fingerprint_timeline - _fingerprint_actived));
            }
        },
        error: function (xhr, status, error) {
            oper_flag.value = "ren-zheng-shen-fen";
            pass_btn_text.innerHTML = "認證身份";

            clearInterval(fingerprintActive);
            container.classList.remove('active');
            $(".container").removeClass("container-in"); //关闭指纹动画
            $(".profile .pass-emoji").hide(); // 头像隐藏
            $("#pass-emoji-login").show(); // 灰色头像显示
            code_input.value = "";

            var errorMessage = "请求失败，请稍后再试。";
            if (xhr.status === 0) {
                errorMessage = "网络连接失败，请检查网络设置。";
            } else if (xhr.status === 404) {
                errorMessage = "请求的资源不存在。";
            } else if (xhr.status === 500) {
                // var responseText = JSON.parse(xhr.responseText);
                errorMessage = "服务器异常，请联系技术人员。";
            } else if (error === "timeout") {
                errorMessage = "请求超时，请稍后再试。";
            }
            // 显示错误提示给用户
            $.modal.alertError(errorMessage);
        }
    });
}
function doLoginResultHandle(res) {
    clearInterval(fingerprintActive);
    container.classList.remove('active');
    if (res.code == web_status.SUCCESS) {
        pass_btn_text.innerHTML = "✔"; // 倒计时按钮
        $("#fw-login-msg").removeClass().addClass("am-text-success").text("认证成功"); // 提示
        $(".profile .pass-emoji").hide(); // 头像隐藏
        $("#pass-emoji-yay").show(); // 认证成功表情显示

        startScroll();

        setTimeout(function () {
            $(".container").removeClass("container-in"); //关闭指纹动画
            $(".profile .pass-emoji").hide(); // 头像隐藏
            $("#pass-emoji-user").show(); // 彩色头像显示
            $("#fw-login-msg").removeClass().text(""); // 提示
            $("#fw-login-msg").hide(); // 提示
            $(".fw-sys-msg").show();
            // pass_btn_text.innerHTML = "   進入系統(" + 10 + "s)"; // 倒计时按钮

            /*setTimeout(function () {
                $("#fw-login-msg").removeClass().text(""); // 提示
                $("#fw-login-msg").hide(); // 提示
                startScroll();
                setTimeout(function () {
                    // 倒计时后可进入系统
                    countdown();
                }, 1);
            }, 1);*/

        }, 1300);

        oper_flag.value = "jin-ru-xi-tong";
        pass_btn_text.innerHTML = "進入系統"; // 倒计时按钮
    } else {
        pass_btn_text.innerHTML = "✘"; // 倒计时按钮
        $("#fw-login-msg").removeClass().addClass("am-text-danger").text(res.msg); // 提示
        $(".ticket2 .password").focus(); //token输入获取焦点
        $(".profile .pass-emoji").hide(); // 头像隐藏
        $("#pass-emoji-sad").show(); // 彩色头像显示

        setTimeout(function () {
            $("#fw-login-msg").removeClass().text("");

            $(".container").removeClass("container-in"); //关闭指纹动画
            $(".profile .pass-emoji").hide(); // 头像隐藏
            $("#pass-emoji-login").show(); // 灰色头像显示
            code_input.value = "";
        }, 1300);

        oper_flag.value = "ren-zheng-shen-fen";
        pass_btn_text.innerHTML = "認證身份";
    }
}
/**** 登录认证结束 ****/



/**** 倒计时进入系统动画开始 ****/
function countdown() {
    pass_btn_text.innerHTML = "   進入系統(" + wait + "s)"; // 倒计时按钮
    wait--;
    if (wait > -2) {
        setTimeout(function () {
                countdown();
            },
            1000);
    } else {
        $(".pass-btn").attr("disable", "false"); //按钮可用
        $(".pass-btn").css("background-color", "#000"); //按钮背景恢复正常
        $(".pass-btn").css("cursor", "pointer"); //按钮改为手指状态
        pass_btn_text.innerHTML = "進入系統";
        location.href = ctx + 'osfw/index';
    }
}
/**** 倒计时进入系统动画结束 ****/



/**** 温馨提示滚动动画开始 ****/
function startScroll() {
    //获得当前<ul>
    var $uList = $(".scroll-box ul");
    var timer = null;
    //触摸清空定时器
    $uList.hover(function () {
            clearInterval(timer);
        },
        function () { //离开启动定时器
            timer = setInterval(function () {
                    if (oper_flag.value == "jin-ru-xi-tong") {
                        scrollList($uList);
                    }
                },
                2000);
        }).trigger("mouseleave"); //自动触发触摸事件
}
//滚动动画
function scrollList(obj) {
    //获得当前<li>的高度
    var scrollHeight = obj.find("li:first").height();
    //滚动出一个<li>的高度
    obj.stop().animate({
            marginTop: -scrollHeight
        },
        600,
        function () {
            //动画结束后，将当前<ul>marginTop置为初始值0状态，再将第一个<li>拼接到末尾。
            obj.css({
                marginTop: 0
            }).find("li:first").appendTo(obj);
        });
}
/**** 温馨提示滚动动画结束 ****/



/**** 验证码输入限制开始 ****/
function maxLengthCheck(object) {
    if (object.value.length > object.max.length)
        object.value = object.value.slice(0, object.max.length)
}
function isNumeric(evt) {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode(key);
    var regex = /[0-9]|\./;
    if (!regex.test(key)) {
        theEvent.returnValue = false;
        if (theEvent.preventDefault) theEvent.preventDefault();
    }
}
function isMoreThan(evt) {
    var theEvent = evt || window.event;
    var target = theEvent.target;
    var keyID = event.keyCode;
    var isDelete = false;
    switch (keyID) {
        case 8:
            isDelete = true;
            // alert("backspace");
            break;
        case 46:
            isDelete = true;
            // alert("delete");
            break;
        default:
            break;
    }

    if (!isDelete && target.value.length == target.max.length) {
        return false;
    }
}
/**** 验证码输入限制结束 ****/

